<template>
  <div class="login-container">
    <el-form
      class="login-form"
      ref="login-form"
      :model="user"
      :rules="formRules"
    >
      <div class="login-head"></div>
      <el-form-item prop="mobile">
        <el-input v-model="user.mobile" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input v-model="user.code" placeholder="请输入验证码"></el-input>
      </el-form-item>
      <el-form-item prop="agree">
        <el-checkbox v-model="user.agree"
          >我已阅读并同意用户协议和隐私条款</el-checkbox
        >
      </el-form-item>
      <el-form-item>
        <el-button
          class="login-btn"
          type="primary"
          @click="onLogin"
          :loading="loginLoading"
          >登陆</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login } from "@/api/user.js";
export default {
  name: "LoginIndex",
  data() {
    return {
      user: {
        mobile: "13911111111", //手机号
        code: "246810", //验证码
        agree: false //是否同意协议
      },
      // checked: false, //协议选中状态
      loginLoading: false, //登陆状态
      formRules: {
        //表单验证规则配置
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "change" },
          {
            pattern: /^1[3|5|7|8|9]\d{9}$/,
            message: "请输入正确的手机号码格式",
            trigger: "change"
          }
        ],
        code: [
          { required: true, message: "验证码不能为空", trigger: "change" },
          {
            pattern: /^\d{6}$/,
            message: "请输入正确的验证码格式",
            trigger: "change"
          }
        ],
        agree: [
          {
            //自定义校验规则
            validator: (rule, value, callback) => {
              if (value) {
                callback();
              } else {
                callback(new Error("请同意用户协议"));
              }
            },
            // message: "请勾选同意用户协议",
            trigger: "change"
          }
        ]
      }
    };
  },
  methods: {
    onLogin() {
      //表单验证
      this.$refs["login-form"].validate(valid => {
        if (!valid) {
          return;
        }
        this.login();
      });
    },
    login() {
      //开启登陆中loading，防抖节流
      this.loginLoading = true;
      //发送请求
      login(this.user)
        .then(res => {
          //登陆成功
          this.$message({
            message: "登陆成功",
            type: "success"
          });
          //关闭loading
          this.loginLoading = false;
          //将接口返回的用户相关数据放到本地存储，方便应用数据共享
          window.localStorage.setItem("user", JSON.stringify(res.data.data));
          // 跳转到首页
          this.$router.push({
            name: "home"
          });
        })
        .catch(err => {
          //登陆失败
          this.$message.error("登陆失败，手机号或验证码错误");
          //关闭loading
          this.loginLoading = false;
        });
    }
  }
};
</script>

<style lang="less" scoped>
.login-container {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url(./login_bg.jpg) no-repeat;
  background-size: cover;
  .login-head {
    width: 300px;
    height: 57px;
    margin-bottom: 10px;
    margin-left: 30px;
    background: url(./logo_index.png) no-repeat;
  }
  .login-form {
    min-width: 200px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-btn {
      width: 100%;
    }
  }
}
</style>
